import threading,Queue
import urllib2,urllib
from lowlevel import *
from midlevel import *
class Ant(threading.Thread):
	pile = Queue.Queue()
        local={}
        locallock=threading.Lock()
        worklock=threading.Lock()
        workersout=0
	def run(self):
            try:
                while True:
                    task=type(self).pile.get(True,1)
                    self.leave()
                    self.work(task)
                    type(self).pile.task_done()
                    self.comehome()
            except Queue.Empty:
                    return
	def fill_local(self):
                try:
                        with open(filename(self)+'.txt','r') as reference:
                                local_content=reference.read()
                        while (local_content!=""):
                                temp1=local_content.partition("\n")
                                temp2=temp1[0].partition("\r")
                                type(self).local[temp2[0]]=temp2[2]
                                local_content=temp1[2]
                except:
                        print "No reference file"
        def save_file(self,audio,item):
                try:
                    audio.save()
                    print "file written"
                    del audio
                    return
                except:
                    print "wtf"
                    del audio
                    type(self).pile.put(item)
                    return
                
	def check_local(self,item):
                item=su(item.lower())
                if item in type(self).local:
                    if(type(self).local[item]=="work"):
                        type(self).pile.put(item)
                        return "delay"
                    elif (type(self).local[item] =="closed"):
                        return "fail"
                    else:
                        return type(self).local[item]                     
            
                                        
                else:
                    type(self).locallock.acquire()
                    type(self).local[item] ="work"
                    type(self).locallock.release()
                    return "work"
        def fail_local_entry(self,what):
            type(self).locallock.acquire()
            print "not in database"
            type(self).local[su(what)]="closed"
            type(self).locallock.release()
        def rem_local_entry(self,what):
            type(self).locallock.acquire()
            print "unknown error"
            del type(self).local[su(what)]
            type(self).locallock.release()
        def suc_local_entry(self,what,value):
            type(self).locallock.acquire()
            print "success"
            type(self).local[su(what)]=su(value)
            type(self).locallock.release() 
        def leave(self):
		type(self).worklock.acquire()
		type(self).workersout=type(self).workersout+1
		type(self).worklock.release()
	def comehome(self):
		type(self).worklock.acquire()
		type(self).workersout=type(self).workersout-1
		type(self).worklock.release()
